#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>

#define MAX 10000000
double step;            //must be odd
double sum = 0;

void* thread(void* arg)
{
    double* result = malloc(sizeof(double));
    *result = 0;
    double begin = step*2 + 1;
    for (int i = 0; i < step; i++)
    {
        if (i&1)
            *result -= 1/begin;
        else
            *result += 1/begin;
        begin += 2;
    }

    return result;
}

int main(int argc, char** argv)
{
    assert(argc == 1);

    int s = MAX/2;
    if (s&1)
        s++;
    step = s;

    pthread_t* worker;
    pthread_create(worker, NULL, thread, NULL);

    double begin = 1;
    for (int i = 0; i < step; i++)
    {
        if (i&1)
            sum -= 1/begin;
        else
            sum += 1/begin;
        begin += 2;
    }

    double* result;
    pthread_join(*worker, (void **)&result);

    sum += *result;
    free(result);

    printf("PI = %f", sum*4);
}